package com.ynu.springbootappdemo.controller;

import com.ynu.springbootappdemo.common.controller.LogicController;
import com.ynu.springbootappdemo.common.exception.BusinessException;
import com.ynu.springbootappdemo.common.utils.BaseResponse;
import com.ynu.springbootappdemo.common.utils.ErrorCode;
import com.ynu.springbootappdemo.common.utils.ResultUtils;
import com.ynu.springbootappdemo.dao.ICityDao;
import com.ynu.springbootappdemo.model.dto.city.CityAddRequest;
import com.ynu.springbootappdemo.model.dto.file.UploadFileRequest;
import com.ynu.springbootappdemo.model.entity.City;
import com.ynu.springbootappdemo.model.vo.CityVo;
import com.ynu.springbootappdemo.service.CityService;
import com.ynu.springbootappdemo.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "城市控制器")
@RestController
@RequestMapping("/city")
public class CityController extends LogicController<CityService, ICityDao, City,Long> {
    public CityController(@Autowired CityService service) {
        super(service);
    }

    @Resource
    private ModelMapper modelMapper;

    @Resource
    private UploadService uploadService;

    /**
     *
     * 获取城市列表
    * */
    @GetMapping("/getAll")
    @ApiOperation("获取城市列表")
    public BaseResponse<List<CityVo>> getAll(){
        return new BaseResponse<>(0, service.getAllCity(), "获取成功");
    }
    /**
     *
     *  添加城市(内部测试)
     * @Param CityAddRequest
     * @Return BaseResponse<CityVo>
     * */
    @PostMapping("/inner/add")
    @ApiOperation("添加城市(内部测试)")
    public BaseResponse<CityVo> addInner(@ApiParam("城市添加对象") CityAddRequest cityAddRequest){
        try {
            City city = modelMapper.map(cityAddRequest, City.class);
            //添加图片
            String fileUrl = uploadService.uploadFile(cityAddRequest, city.getCityName());
            //设置图片
            city.setImageUrl(fileUrl);
            //添加信息
            CityVo cityVo = service.save(city);
            return ResultUtils.success(cityVo);
        }catch (Exception e){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
    }

}
